var tools = require("../../common/Tools.js").tools;
var cache = require("../../common/Cache.js").cache;
var dataFilter = require("../../common/DataFilter.js").dataFilter;
// var replacePictures = require("../../common/replacePictures.js");

const app = getApp();

// 按揭更多资讯
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowLoading:false,
    isend:false,
    bank_isend:false,
    bankPage:1,
    bankPagesize:10,
    bankNewsCount:"",
    newsCount:"",
    DataList:[],
    bankNewsList:[],
    PageIndex:1,
    pagesize:10,
    clientHeight:'',
    size: 1,
    navbarActiveIndex: 0,
    navbarTitle: [
      "樓市指南",
      "銀行公告"
    ]
  },
  
  scrollbot_bank(){
    if(this.data.bankNewsList.length >= this.data.bankNewsCount){
      this.setData({
        bank_isend:true
      })
      return false
    } 
    this.data.bankPage++
    this.loadBankList()
  },


  scrollbot(){
    // console.log("987",this.data.DataList.length)
    // console.log("987",this.data.newsCount)
    if(this.data.DataList.length >= this.data.newsCount){
      this.setData({
        isend:true
      })
      return false
    }
    this.data.PageIndex++
    this.loadList()
  },


  /**
   * 点击导航栏
   */
  onNavBarTap: function (event) {
    // 获取点击的navbar的index
    let navbarTapIndex = event.currentTarget.dataset.navbarIndex
    // 设置data属性中的navbarActiveIndex为当前点击的navbar
    this.setData({
      navbarActiveIndex: navbarTapIndex
    })
    if(navbarTapIndex === 0){
      this.fenderingData("#newList",this.data.DataList)
    }else{
      this.fenderingData("#bankNewList",this.data.bankNewsList)
    }
  },

  // 列表组件渲染数据
  fenderingData:function(el,data){
    var conponment = this.selectComponent(el);
    if(conponment!==null){
      conponment.setData({
        DataList:data
      })
    }
  },

  onBindAnimationFinish: function ({detail}) {
    // 设置data属性中的navbarActiveIndex为当前点击的navbar
    this.setData({
        navbarActiveIndex: detail.current
    })
    if(detail.current === 0){
      // this.fenderingData("#newList",this.data.DataList)
      // var c_newList = this.selectComponent("#newList");
      // c_newList.setData({
      //   DataList:this.data.DataList
      // })
    }else{
      // this.fenderingData("#bankNewList",this.data.bankNewsList)
    }
  },


  loadList:function(){
    
    wx.showLoading({
      title: '正在加載',
    })
    this.setData({
      isShowLoading:true
    })
    var that = this;
    
    var c_newList = this.selectComponent("#newList");
    // var bank_newList = this.selectComponent("#bankNewList");
   
    /*获取首页按揭文章*/
    wx.request({
      // url: app.macauweburl.webApiUrl + "AppArticle/GetList",
      // url: "http://10.68.2.9/CenMacauCMS2/api/Article/GetList",
      url: app.macauweburl.webApiUrl + "Article/GetList",
      data: {
        Type: 'EX',
        PageIndex: this.data.PageIndex,
        pagesize: this.data.pagesize
      },
      success(res) {
       
        var allArticle = res.data.InnerList;

        if (allArticle != undefined) {
          var filterArticle = dataFilter.getFilter("Type", "EX", allArticle);

          for (var i = 0; i < filterArticle.length; i++) {
            filterArticle[i].Path = filterArticle[i].Images[0];
            // console.log(filterArticle[i].Images[0])
          }
          
          var list = [].concat(that.data.DataList,filterArticle)
          
          that.setData({
            DataList: list
          });
          if(c_newList!==null){
            c_newList.setData({
              DataList: list
            });
          }
        }
        if(c_newList===undefined)return

        /*绑定点击事件*/
        c_newList.onItemTap(function (event) {
          // app.console.log(event);
          var id = event.currentTarget.dataset.obj.Code;
          // console.log(event.currentTarget.dataset.obj.Code)

          var v = Date.parse(new Date());
          wx.navigateTo({
            url: '/pages/ArticleDetails/ArticleDetails?id=' + id + "&v=" + v,
          });
        });
        setTimeout(function () {
          if(that.data.isShowLoading){wx.hideLoading()}
          that.setData({isShowLoading:false})
        }, 100)
      }
    })
  },

  loadBankList:function(){
    wx.showLoading({
      title: '正在加載',
    })
    this.setData({
      isShowLoading:true
    })
    var that = this;
    
    var bank_newList = this.selectComponent("#bankNewList");
    // var c_newList = this.selectComponent("#newList");

    wx.request({
      // url: app.macauweburl.webApiUrl + "AppArticle/GetList",
      // url: "http://10.68.2.9/CenMacauCMS2/api/AppArticle/GetList",
      url: app.macauweburl.webApiUrl + "AppArticle/GetList",
      data: {
        Type: '402',
        // PageIndex: this.data.bankPage,
        // pagesize: this.data.bankPagesize
      },
      success(res) {
        var allArticle = res.data.InnerList;
        if (allArticle != undefined) {
          var filterArticle = dataFilter.getFilter("Type", "402", allArticle);
          
          // for (var i = 0; i < filterArticle.length; i++) {
          //   filterArticle[i].Path = filterArticle[i].Images[0].split(";")[0];
          // }
          that.setData({
            bankNewsCount: res.data.TotalCount
          });
          var list = [].concat(that.data.bankNewsList,filterArticle)
          that.setData({
            bankNewsList: list
          });
          
          if(bank_newList!==null){
            bank_newList.setData({
              DataList: list
            });
          }
        }
          bank_newList.onItemTap( function(event){
          console.log(event)
          app.console.log(event);
          // var id = event.currentTarget.dataset.obj.ID;
          var id = event.currentTarget.dataset.obj.ID;

          var v = Date.parse(new Date());
          wx.navigateTo({
            url: '/pages/BankArticleDetails/BankArticleDetails?id=' + id + "&v=" + v,
          });
        });
        
        setTimeout(function () {
          if(that.data.isShowLoading){wx.hideLoading()}
          that.setData({isShowLoading:false})
        }, 100)
      }
    })
  },
    

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 加载银行数据
    this.loadBankList()

    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight-50
        });
      }
    })

    wx.showLoading({
      title: '正在加載',
    })
    this.setData({
      isShowLoading:true
    })
    var c_newList = this.selectComponent("#newList");

    /*获取首页按揭文章*/
    wx.request({
      // url: app.macauweburl.webApiUrl + "AppArticle/GetList",
      // url: "http://10.68.2.9/CenMacauCMS2/api/Article/GetList",
      url: app.macauweburl.webApiUrl + "Article/GetList",
      data: {
        Type: 'EX',
        PageIndex: this.data.PageIndex,
        pagesize: this.data.pagesize
      },
      success(res) {
        // for (var j = 0; j < res.data.InnerList.length;j++){
        //   res.data.InnerList[j].Images[0] = replacePictures.clearInvalidStyle(res.data.InnerList[j].Images[0]);

        // }

        // console.log(res.data)
        // console.log('==================================');
        var allArticle = res.data.InnerList;

        if (allArticle != undefined) {
          var filterArticle = dataFilter.getFilter("Type", "EX", allArticle);

          for (var i = 0; i < filterArticle.length; i++) {
            filterArticle[i].Path = filterArticle[i].Images[0];
            // console.log(filterArticle[i].Images[0])

          }
          that.setData({
            newsCount:res.data.TotalCount
          })
          that.setData({
            DataList:filterArticle
          })
          if(c_newList!==null){
            c_newList.setData({
              DataList: filterArticle
            });
          }
        }

        /*绑定点击事件*/
        c_newList.onItemTap(function (event) {

          app.console.log(event);
          var id = event.currentTarget.dataset.obj.Code;
          // console.log(event.currentTarget.dataset.obj.Code)

          var v = Date.parse(new Date());
          wx.navigateTo({
            url: '/pages/ArticleDetails/ArticleDetails?id=' + id + "&v=" + v,
          });

        });
        setTimeout(function () {
          if(that.data.isShowLoading){wx.hideLoading()}
          that.setData({isShowLoading:false})
        }, 100)
      }
    })


    /*cache.get("allArticle", function (result) {
      var allArticle = result;

      if (allArticle != undefined) {
        var filterArticle = dataFilter.getFilter("Type", "356", allArticle);

        c_newList.setData({
          DataList: filterArticle
        });
      } 

      /绑定点击事件0/
      c_newList.onItemTap(function (event) {
        
        app.console.log(event);
        var id = event.currentTarget.dataset.id;
        var v=Date.parse(new Date());
        wx.navigateTo({
          url: '/pages/ArticleDetail/ArticleDetail?id=' + id+"&v="+v,
        });
        
      });

    }); */
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottomsss: function () {
    wx.showLoading({
      title: '正在加載',
    })
    this.setData({
      isShowLoading:true
    })
    var that = this;
    var size = this.data.size + 1;
    that.setData({
      size: size
    })
    // console.log(size);


    // console.log(this.data.size);


    var that = this;
    var c_newList = this.selectComponent("#newList");

    /*获取首页按揭文章*/
    wx.request({
      // url: app.macauweburl.webApiUrl + "AppArticle/GetList",
      url: app.macauweburl.webApiUrl + "Article/GetList",
      // url: "http://10.68.2.9/CenMacauCMS2/api/Article/GetList",
      data: {
        Type: 'EX',
      },
      success(res) {
        for (var j = 0; j < res.data.InnerList.length; j++) {
          res.data.InnerList[j].Images[0] = replacePictures.clearInvalidStyle(res.data.InnerList[j].Images[0]);

        }

        // console.log(res.data)
        // console.log('==================================');
        var allArticle = res.data.InnerList;

        if (allArticle != undefined) {
          var filterArticle = dataFilter.getFilter("Type", "EX", allArticle);

          for (var i = 0; i < filterArticle.length; i++) {
            filterArticle[i].Path = filterArticle[i].Images[0];

          }
          // console.log(c_newList)
          c_newList.setData({
            DataList: filterArticle
          });
        }

        /*绑定点击事件*/
        c_newList.onItemTap(function (event) {

          // app.console.log(event);
          var id = event.currentTarget.dataset.obj.Code;
          // console.log(event.currentTarget.dataset.obj.Code)

          var v = Date.parse(new Date());
          wx.navigateTo({
            url: '/pages/ArticleDetails/ArticleDetails?id=' + id + "&v=" + v,
          });

        });
        setTimeout(function () {
          if(that.data.isShowLoading){wx.hideLoading()}
          that.setData({isShowLoading:false})
        }, 100)
      }
    })




    //上拉加载
    wx.stopPullDownRefresh();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})